package pers.wq.model;

import java.util.List;
import java.util.Map;

import pers.wq.model.dao.core.BeanDao;
import pers.wq.model.dao.core.QueryDao;
import pers.wq.util.StringUtil;

public class MyModel<T> {
	protected String table;
	protected Class c;
	
	public MyModel(String table){
		this.setTable(table);
		this.c = null;
	}
	
	public MyModel(String table,Class c) {
		this.setTable(table);
		this.c = c;
	}
	
	public static boolean save(Object obj){
		return BeanDao.save(obj);
	}
	
	public static boolean update(Object obj){
		return BeanDao.update(obj);
	}
	
	public static boolean delete(Object obj){
		return BeanDao.delete(obj);
	}
	
	public List<T> select(String condition){
		String str = "SELECT * FROM " + this.getTable() + " WHERE " + condition;
		System.out.println(str + "1");
		return QueryDao.query(str, c);
	}
	
	public static List<Map<String,String>> selectMap(String column,String table,String condition,String limit,String order){
		
		if(StringUtil.strIsNull(table)){
			return null;
		}
		
		String str = "SELECT ";
		
		if(StringUtil.strIsNull(column)){
			str += "*";
		}else{
			str += column;
		}
		
		str += " FROM " + table + " WHERE ";
		
		if(StringUtil.strIsNull(condition)){
			str += "1=1";
		}else{
			str += condition;
		}

		if(!StringUtil.strIsNull(order)){
			str += " ORDER BY " + order;
		}
		
		if(!StringUtil.strIsNull(limit)){
			str += " LIMIT " + limit;
		}
		
		
		return QueryDao.query(str);
	}
	
	public static Object select(Class c,int id){
		return BeanDao.select(c, id);
	}
	
	public static boolean checkList(List list) {
		if(null == list) {
			return false;
		}
		return true;
	}

	public String getTable() {
		return table;
	}

	public void setTable(String table) {
		this.table = table;
	}
	
	
}
